#region (c) 2014 THN Solutions LLC. - All rights reserved
/*
THN TELEPORT version 2

Copyright (c) 2014, THN Solutions LLC. ( www.ThnSolutions.com )
Author: Nguyễn, M. Hải                 ( www.Minh-Hai.com     )
All rights reserved.

This library is dual-licensed.
  + For commercial software, please obtain a commercial license from THN Solutions LLC.

  + For free software, this library is licensed under GPL version 3. A summary of GPLv3 is
    listed below. You should also find a copy of GPLv3 as file License_GPLv3.txt included
	with the source-code files.

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License version 3 as published by
    the Free Software Foundation  of the License.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
#endregion

#region Record of Change
//             							CHANGE HISTORY
// -------------------------------------------------------------------------------------
// |   DATE    | DEVELOPER  | DESCRIPTION                                              |
// -------------------------------------------------------------------------------------
// | 31-Dec-09 | HaiNM      | First creation.                                          |
// -------------------------------------------------------------------------------------
//
#endregion

#region Record of Audit
//             							REVIEW HISTORY
// -------------------------------------------------------------------------------------
// |   DATE    | REVIEWER   | NOTES                                                    |
// -------------------------------------------------------------------------------------
// -------------------------------------------------------------------------------------
//
#endregion

#region Using directives
using System;

using Thn;
using Thn.Application.Logging;
#endregion

namespace Thn.Communication.Teleporting.Logging
{
    /// <summary>
    /// A custom implementation of <see cref="ILogListener"/> that uses Teleport for
    /// transfering logs.
    /// </summary>
#if DEBUG
    public
#else
    public
#endif
 class TeleportLogger : ILogListener, ITeleportLoggingService
    {
        #region Fields
        bool mIsInitializedAsServer = false;
        #endregion

        #region Properties

        #region Gateway
        private string mGateway = "TeleportLoggingService";
        /// <summary>
        /// Gets/Sets name of the gateway for Teleport Logging Service.
        /// </summary>
        public string Gateway
        {
            get { return mGateway; }
            set { mGateway = value; }
        }
        #endregion

        #region Teleporter
        private ITeleporter mTeleporter;
        /// <summary>
        /// Gets/Sets the teleporter this connector shall be connecting for.
        /// <para>When unassigned, the connector shall use the default teleporter.</para>
        /// </summary>
        public ITeleporter Teleporter
        {
            get
            {
                ITeleporter result = null;
                if (mTeleporter != null) result = mTeleporter;
                else result = Teleport.Teleporter;

                return result;
            }
            set { mTeleporter = value; }
        }
        #endregion

        #region Sender
        private string mSender = "";
        /// <summary>
        /// Gets/Sets name of the sender.
        /// </summary>
        public string Sender
        {
            get { return mSender; }
            set { mSender = value; }
        }
        #endregion

        #endregion

        #region Configure For Server
        /// <summary>
        /// Setup the log system to receive remote logs.
        /// <para>This method should be called at server side.</para>
        /// </summary>
        public void ConfigureForServer()
        {
            if (!mIsInitializedAsServer)
            {
                TypeResolver.RegisterAllTypes(this.GetType(), "Thn.Communication.Teleporting.Logging.*");
                Teleporter.AddGateway(Gateway, ((ITeleportLoggingService)this));
                mIsInitializedAsServer = true;
            }
            #region Defensive tracing
            else
            {
                //already configured
            }
            #endregion
        }
        #endregion

        #region Configure For Client
        /// <summary>
        /// Setup the log system to send logs to remote party.
        /// <para>This method should be called at client side.</para>
        /// </summary>
        public void ConfigureForClient()
        {
            TypeResolver.RegisterAllTypes(this.GetType(), "Thn.Communication.Teleporting.Logging.*");
            Log.Bind(this);
        }
        #endregion

        #region ILogListener Members

        #region Receive Log
        /// <summary>
        /// Receive a broadcasted log
        /// </summary>
        void ILogListener.ReceiveLog(LogEvent log)
        {
            try
            {
                Teleporter.Request(mGateway, "WriteLog", new LogInfo(log, mSender));
            }
            catch (Exception ex)
            {
            }
        }
        #endregion

        #region New Session
        /// <summary>
        /// Begins a new log session
        /// </summary>
        void ILogListener.NewSession()
        {
        }
        #endregion

        #region Start
        /// <summary>
        /// Begins a new logging scope
        /// </summary>
        /// <param name="scopeName">name of scope</param>
        /// <param name="scopeID">ID of scope</param>
        void ILogListener.Start(string scopeName, object scopeID)
        {
        }
        #endregion

        #region End
        /// <summary>
        /// Ends a logging scope
        /// </summary>
        /// <param name="scopeID">ID of scope to end</param>
        void ILogListener.End(object scopeID)
        {
        }
        #endregion

        #region Update
        /// <summary>
        /// Change the name of a logging scope
        /// </summary>
        /// <param name="scopeID">ID of scope to change</param>
        /// <param name="scopeName">new scope name</param>
        void ILogListener.Update(object scopeID, string scopeName)
        {
        }
        #endregion

        #endregion

        #region ITeleportLoggingService Members
        
        #region Write Log
        /// <summary>
        /// Write a log event.
        /// </summary>
        //void ITeleportLoggingService.WriteLog(LogInfo info)
        public void WriteLog(LogInfo info)
        {
            //Log.Debug("Received remote log");
            Log.WriteLog(info.ToLogEvent());
        }
        #endregion

        #endregion

        #region Constructors
        /// <summary>
        /// Default constructor.
        /// </summary>
        public TeleportLogger()
        {
            //int iii = 0;
        }
        #endregion
    }
}
